package Exam.ArrayListDemo;

import java.util.ArrayList;

public class ArrayListDemo6 {
    /*
    * main定义一个集合，存入三个用户对象
    * 用户属性为：id, username, password
    * 定义一个方法，根据id查找对应的用户信息。
    * 如果存在，返回true
    * 否则返回false
    * */
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();

        User u1 = new User("001","zhangsan","123456");
        User u2 = new User("002","lisi","123456");
        User u3 = new User("003","wangwu","123456");

        list.add(u1);
        list.add(u2);
        list.add(u3);

        boolean flag = contains(list,"001");
        System.out.println(flag);

        boolean flag10 = contains(list,"010");
        System.out.println(flag10);
    }

    public static boolean contains(ArrayList<User> list, String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                //如果找到了直接返回true
                return true;
            }
        }
        //循环结束，表示所有结果比较完毕，没找着
        return false;
    }
}
